package org.teremail.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Utility class for handling formatting of various objects, including dates,
 * numbers etc.
 * 
 * @author Michael Barker
 *
 */
public class Formats {

    
    private static ThreadLocalCache<String,SimpleDateFormat> DATE_FORMAT_CACHE = 
        new ThreadLocalCache<String,SimpleDateFormat>() {

        @Override
        public SimpleDateFormat create(String k) {
            return new SimpleDateFormat(k);
        }

    };
    
    /**
     * Formats a date.  This method will cache the formats in a thread local
     * cache for performance and thread safety.
     * 
     * @param format
     * @param date
     * @return
     */
    public static String formatDate(String format, Date date) {
        return DATE_FORMAT_CACHE.getValue(format).format(date);
    }
    
    /**
     * Parses a date.  This method will cache the formats in a thread local
     * cache for performance and thread safety.
     * 
     * @param format
     * @param date
     * @return
     */
    public static Date parseDate(String format, String dateStr) throws ParseException {
        return DATE_FORMAT_CACHE.getValue(format).parse(dateStr);
    }
    
    public static Date parseSMTPDate(String dateStr) {
        return new Date();
    }
    
}
